<?php
class Admin_LienHeController extends Zend_Controller_Action
{

    public function init()
    {
          $this->_helper->layout->setLayout('admin');
    }

    public function indexAction()
    {
    	$lienheMapper = Library_Model_LienHeMapper::getInstance();
    	$lienhe = $lienheMapper->lienheList();   
    	$this->view->lienhe = $lienhe;
    	
    	Zend_Registry::get('logger')->info($lienhe);
    }

    public function themAction()
    {
    	$this->view->title = 'Hỏi đáp - Góp ý  ?';
    	$formThem = new Admin_Form_LienHeForm();
    	$formThem->setMethod('post');
    	$formThem->setAction('/zfMHB/public/admin/lienhe/them');
    	
    	$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
    	
    	$this->view->form = $formThem;
    	
    	if($this->getRequest()->isPost()){
    		if($formThem->isValid($_POST)){
    			$data = $formThem->getValues();
    			
    			$hovaten 		= $data['hovaten'];
    			$diachi 			= $data['diachi'];
    			$email		= $data['email'];
    			$linhvuc 			= $data['linhvuc'];
    			$tieude			= $data['tieude'];
				$noidung		= $data['noidung'];
				
    			$lienheMapper = Library_Model_LienHeMapper::getInstance();
    			$lienheObj 	= new Library_Model_LienHe();
    			    			
    			$lienheObj->setHovaten($hovaten);
    			$lienheObj->setDiachi($diachi);
    			$lienheObj->setEmail($email);
    			$lienheObj->setLinhvuc($linhvuc);
				$lienheObj->setTieude($tieude);
				$lienheObj->setNoidung($noidung);
				
    			$lienhe = $lienheMapper->save($lienheObj);
    			$this->_redirect('/admin/lienhe/index');
    		}
    	}
    }

    public function capnhatAction()
    {
        $this->view->title = 'Cập nhật câu hỏi';
        $formCapnhat = new Admin_Form_LienHeForm();
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/lienhe/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
        $malienhe = $this->getRequest()->getParam('id');
        
        
        $lienheMapper = Library_Model_LienHeMapper::getInstance();
        $this->view->form = $formCapnhat;
        
        if($this->getRequest()->isPost()){
        	if($formCapnhat->isValid($_POST)){
        		$data = $formCapnhat->getValues();
        		
        		$id 					= $data['id'];
        		$hovaten 		= $data['hovaten'];
    			$diachi 			= $data['diachi'];
    			$email		= $data['email'];
    			$linhvuc 			= $data['linhvuc'];
    			$tieude			= $data['tieude'];
				$noidung		= $data['noidung'];
				
    			
    			$lienheObj 	= new Library_Model_LienHe();

    			$lienheObj->setId($id);
    			$lienheObj->setHovaten($hovaten);
    			$lienheObj->setDiachi($diachi);
    			$lienheObj->setEmail($email);
    			$lienheObj->setLinhvuc($linhvuc);
				$lienheObj->setTieude($tieude);
				$lienheObj->setNoidung($noidung);
				
    			$lienhe = $lienheMapper->save($lienheObj);
    			$this->_redirect('/admin/lienhe/index');
			
        	}
        } else {
        		$lienhe = $lienheMapper->searchLienHeById($malienhe);
        		
        		$formCapnhat->getElement('id')->setValue($lienhe->getId());
        		$formCapnhat->getElement('hovaten')->setValue($lienhe->getHovaTen());        		
        		$formCapnhat->getElement('diachi')->setValue($lienhe->getDiachi());
        		$formCapnhat->getElement('email')->setValue($lienhe->getEmail());
        		$formCapnhat->getElement('linhvuc')->setValue($lienhe->getLinhvuc());
				$formCapnhat->getElement('tieude')->setValue($lienhe->getTieude());
				$formCapnhat->getElement('noidung')->setValue($lienhe->getNoidung());
				
			}
        
    }

    public function xoaAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
        $malienhe = $this->getRequest()->getParam('id');
        $lienheMapper = Library_Model_LienHeMapper::getInstance();
        $lienheMapper->delete($malienhe);
        $this->_redirect('/admin/lienhe/index');
    }
    
}









		
		
		
		
		
		
		